/*
* @Author: xfy
* @Date:   2017-09-13 15:23:20
* @Last Modified by:   xfy
* @Last Modified time: 2017-09-13 20:48:47
*/
require('./index.css');
require('page/common/admin-nav-top/index.js');
var _supper         = require('util/supper.js');
var _productManage  = require('service/admin-product-service.js');
var _orderManage    = require('service/admin-order-service.js');
var Pagination      = require('util/pagination/index.js');
var templateIndex   = require('./index.string');
var templateDetail   = require('./detail.string');

//page逻辑部分
var page = {
    data : {
        listParam : {
            pageNum     : 1,
            pageSize    : 10
        }
    },
    init : function(){
        this.loadOrderList();
        this.bindEvent();
    },
    bindEvent : function(){
        var _this           = this;
        //点击订单号和查看详情到订单详情页
        $(document).on('click', '.a.order-detail', function(e){
            var orderNum = $(e.target).data('orderNo');
            _this.loadOrderDetail(orderNum);
        });
        //点击搜索订单号
        $(document).on('click', '.admin-order-input', function(){
            var orderNum    = $('.admin-order-search').val(),
            //参数验证
            result          = _this.validata({
                orderNum : orderNum
            });
            //通过验证
            if(result.isTrue && result.orderNum){
                _orderManage.manageSearch(result.orderNum, function(res){
                    var orderManageHtml = _supper.renderHtml(templateIndex, {
                        list : res
                    });
                    _this.$orderCon.html(orderManageHtml);

                    //搜索框回填
                    $('.admin-order-search').val(result.orderNum);
                }, function(errMsg){
                    $('.order-body').html('<div class="error-tip"><p class="error-msg"><i class="fa fa-coffee"></i> 没有订单哦…</p></div>');
                });
            }
            //通过验证但是订单号为空
            else if(result.isTrue && !result.orderNum){
                _this.loadOrderList();
            }
            //不为数字
            else{
                _supper.errorTips(result.errMsg);
                return;
            }
        });
        //查看详细订单时的返回按钮事件
        $(document).on('click', '.order-detail-back', function(){
            _this.loadOrderList();
        });
        //发货按钮事件
        $(document).on('click', '.send-goods', function(){
            var orderNum = $('#orderNo').data('orderno');
            _orderManage.manageSendGoods(orderNum, function(res){
                _supper.successTips(res);
                _this.loadOrderDetail(orderNum);
            }, function(errMsg){
                _supper.errorTips(errMsg);
            });
        });
    },
    //参数验证
    validata : function(data){
        var result = {
            isTrue : false
        };
        result.orderNum       = data.orderNum || '';

        //如果为空则显示全部
        if(result.orderNum === ''){
            result.isTrue = true;
            return result;
        }

        else if(!$.isNumeric(result.orderNum)){
            result.errMsg = '订单号必须为数字';
            return result;
        }

        result.isTrue = true;
        return result;
    },
    //加载订单详情页
    loadOrderDetail : function(orderNum){
        var _this           = this;
        this.$orderCon.html('<div class="loading"></div>');
        _orderManage.orderDetail(orderNum, function(res){
            var orderDetailHtml = _supper.renderHtml(templateDetail, res);
            _this.$orderCon.html(orderDetailHtml);
            //发货按钮的添加
            var status = $('.send-goods').data('desc');
            if(status === '已支付'){
                $('.send-goods').show();
            }
        }, function(errMsg){
            _this.$orderCon.html('<div class="error-tip"><p class="error-msg"><i class="fa fa-frown-o"></i> 好像出错了哦…</p></div>');
        });
    },
    //加载品类的信息
    loadOrderList : function(){
        var _this           = this,
            $orderCon       = $('.order-con');
        this.$orderCon      = $orderCon;
        $orderCon.html('<div class="loading"></div>');
        _orderManage.orderList(this.data.listParam, function(res){
            var orderManageHtml = _supper.renderHtml(templateIndex, res);
            $orderCon.html(orderManageHtml);
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,
                pageNum         : res.pageNum,
                pages           : res.pages
            });
        }, function(errMsg){
            $listCon.html('<div class="error-tip"><p class="error-msg"><i class="fa fa-frown-o"></i> 好像出错了哦…</p></div>');
        });
    },
    //加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pageNum){
                _this.data.listParam.pageNum = pageNum;
                _this.loadOrderList();
            }
        }));
    }
};
$(function(){
    page.init();
});